/**
 * @author 陈泰来
 * @version V1.0
 * @Project echo-java-base
 * @Title 同步阻塞生产者
 * @Description *
 * @Package concurrent.blockingQueue
 * @date 2021/3/19 1:50 下午
 * @Copyright:学如逆水行舟，不进则退 .
 */
package concurrent.blockingQueue.bqtest;

import java.util.concurrent.BlockingQueue;

public class Producer extends Thread {

    public static final int MAX_QUEUE_SIZE = 100;
    BlockingQueue<String> blockingDeque;

    private Integer id = 1;

    //构造
    public Producer(BlockingQueue<String> blockingDeque) {
        this.blockingDeque = blockingDeque;
    }

    @Override
    public void run() {
        try {


            String item = "item";
            String msg;
            for (; ; ) {
//                System.out.println("[producer] 队列当前大小:" + blockingDeque.size());

                if (blockingDeque.size() >= MAX_QUEUE_SIZE) {
                    System.out.printf("[producer] !!! 队列当前尺寸 %d: ，已满，暂时不推消息\n", blockingDeque.size());
                    continue;
                }


                msg = item + id;
                id++;
                blockingDeque.put(msg);
                System.out.println("[producer] 当前生产者线程:" + Thread.currentThread().toString());
                System.out.println("[Producer] 生产者推送消息:" + msg);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
